﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class ContentDao
    {


        public ContentDao()
        {

            Mapper.CreateMap<E_Content, ContentModel>();
        }
        public List<E_Content> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Content.Count();

                return edm.E_Content.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ContentModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = from s in edm.E_Content
                            from m in edm.E_Nav
                            where s.NavId == m.Id
                            && s.Id==id
                            select new ContentModel() { 
                                Id=s.Id,
                                SiteID=s.SiteID,
                                Click=s.Click,
                                Title=s.Title,
                                Content=s.Content,
                                navTitle=m.NavName,
                                CreateTime=s.CreateTime,
                                CreatUserId=s.CreatUserId,
                                CreatUserName=s.CreatUserName,
                                Description=s.Description,
                                EDesc=s.EDesc,
                                EContent=s.EContent,
                                EKeyWord=s.EKeyWord,
                                ETitle=s.ETitle,
                                Flags=s.Flags,
                                Income=s.Income,
                                KeyWord=s.KeyWord,
                                NavId=s.NavId,
                                PicPath=s.PicPath,
                                TitleColor=s.TitleColor,
                                UpdateTime=s.UpdateTime,
                                Weight=s.Weight,
                            };




                //var query = edm.E_Content.FirstOrDefault(s => s.Id == id);
                return query.FirstOrDefault();
            }

        }


        public int Save(ContentModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Content.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Content();

                    edm.AddToE_Content(query);

                }


                query.Title = obj.Title;

                query.ETitle = obj.ETitle;

                query.TitleColor = obj.TitleColor;

                query.Flags = obj.Flags;

                query.Weight = obj.Weight;

                query.PicPath = obj.PicPath;

                query.Income = obj.Income;

                query.NavId = obj.NavId;

                query.KeyWord = obj.KeyWord;

                query.EKeyWord = obj.EKeyWord;

                query.Description = obj.Description;

                query.EDesc = obj.EDesc;

                query.CreateTime = obj.CreateTime;

                query.Content = obj.Content;

                query.EContent = obj.EContent;

                query.CreatUserId = obj.CreatUserId;

                query.CreatUserName = obj.CreatUserName;

                query.UpdateTime = obj.UpdateTime;

                query.Click = obj.Click;

                edm.SaveChanges();

                return query.Id;


            }

        }

        public List<ContentModel> GetNews(int start, int num, int navId)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities()) {
                return Mapper.Map<List< E_Content>,List< ContentModel>>( edm.E_Content.Where(s => s.IsDelete == 0&&s.NavId==navId).OrderByDescending(s=>s.Id).Skip(start).Take(num).ToList());
            }
        }


        public List<ContentModel> GetNews(int startIndex, int maxRow, out int recordcount, int navId)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = edm.E_Content.Where(s => s.IsDelete == 0 && s.NavId == navId).OrderByDescending(s => s.Id);
                recordcount = query.Count();
                return Mapper.Map<List<E_Content>, List<ContentModel>>(query.Skip(startIndex).Take(maxRow).ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Content.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

